package com.fingard.dsp.bank.directbank.mufg01;

import com.fingard.dsp.bank.directbank.bankInterface.IForeignCurrency;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.ReqQryForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.RetAtsForeignCurrSingle;
import com.fingard.dsp.bank.reqrespobj.foreigncurrsingle.detail.RetAtsForeignCurrPayDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.util.ForeignBankUtil;
import com.fingard.xml.XmlTextWriter;

import java.io.File;

public class MUFG01ForeignCurrSinglePay extends MUFG01Base implements IForeignCurrency {

    public static ForeignBankUtil foreignBankHelper=null;

    @Override
    public RetAtsForeignCurrSingle foreignCurrPay(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception {
        RetAtsForeignCurrSingle retAts=null;
        //if("HK".equals(reqForeignCurrSingle.singleDetail.payCtryCode)){
            retAts=foreignPayForHK(reqForeignCurrSingle);
        //}
        return retAts;
    }

    /**
     * 三菱香港支付
     * */
    public RetAtsForeignCurrSingle foreignPayForHK(ReqForeignCurrSingle reqForeignCurrSingle) throws Exception{
        RetAtsForeignCurrSingle retats = new RetAtsForeignCurrSingle(reqForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail tmpRetAtsDetail = new RetAtsForeignCurrPayDetail();
        tmpRetAtsDetail.respCode = "";
        if(tmpRetAtsDetail.respCode.length() == 0) {
            //拼接付款请求报文
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter);
            writeFileContent(xmlWriter,reqForeignCurrSingle);
            //生成付款文件目录
            String payDate=DateHelper.convertD10toD8(reqForeignCurrSingle.singleDetail.payDate);
            String payFilePath = getBankConfig().getValueAsString("PayFilePath")+ File.separator +payDate +File.separator
                    +"PMX3"+payDate.substring(2,8)+"_"+"Transfer"+"_"+reqForeignCurrSingle.singleDetail.askNumber+".txt";
            WriteBankLogLn("请求银行的报文："+xmlWriter.toXmlString());
            foreignBankHelper=new ForeignBankUtil(getBankConfig(),bankFront,getSbLog(),getCharset());
            //生成付款文件并上传
            String[] bankResult=new String[]{"",""};
            if(StringHelper.hasAnyChar(getBankConfig().getValueAsString("TransType"))
                    && getBankConfig().getValueAsString("TransType").equals("true")) {
                //付款文件只加密
                //String bankPubKeyId = getBankConfig().getValueAsString("bankPubKeyId");//银行公钥
                //String coPriKeyId = "";//企业私钥
                //String coPriKeyPsword= "";//企业私钥密码
                //bankResult=foreignBankHelper.uploadPayFile(xmlWriter.toXmlString(),payFilePath,getCharset(),"sftpserver",bankPubKeyId,coPriKeyId,coPriKeyPsword);
                bankResult=foreignBankHelper.uploadPayFileIsEncrypt(xmlWriter.toXmlString(),payFilePath,getCharset(),"sftpserver");
            }else {
                //付款文件不需要加密
                bankResult=foreignBankHelper.uploadPayFile(xmlWriter.toXmlString(),payFilePath,getCharset(),"sftpserver");
            }
            //响应到ats
            tmpRetAtsDetail.reqSeqID=reqForeignCurrSingle.singleDetail.askNumber;
            tmpRetAtsDetail.respCode = "0";
            tmpRetAtsDetail.respInfo = bankResult[1];
            tmpRetAtsDetail.transState = bankResult[0];
            tmpRetAtsDetail.payInfoCode = tmpRetAtsDetail.transState;
            retats.writeRetDetail(tmpRetAtsDetail);
        }
        return retats;
    }

    /**
     * 三菱香港支付状态查询
     * @param reqQryForeignCurrSingle
     * @return
     * @throws Exception
     */
    @Override
    public RetAtsForeignCurrSingle qryForeignCurrPay(ReqQryForeignCurrSingle reqQryForeignCurrSingle) throws Exception {
        RetAtsForeignCurrSingle retats = new RetAtsForeignCurrSingle(reqQryForeignCurrSingle.reqHeader);
        RetAtsForeignCurrPayDetail detail = new RetAtsForeignCurrPayDetail();
        //处理ack文件或者MT文件，获取支付状态  result={"付款状态码","响应信息"}
        foreignBankHelper=new ForeignBankUtil(getBankConfig(),bankFront,getSbLog(),getCharset());
        String[] result=foreignBankHelper.getPayStatus(reqQryForeignCurrSingle.askNumber,reqQryForeignCurrSingle.getBgnDateTime());
        if(result.length==2 && StringHelper.hasAnyChar(result[0])){
            detail.transState = result[0];
            detail.payInfoCode = detail.transState;
            detail.respCode = "0";
            detail.respInfo=result[1];
            detail.reqSeqID=reqQryForeignCurrSingle.askNumber;
        }
        retats.writeRetDetail(detail);
        return retats;
    }

}
